<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <template #header>
        <div class="header">
          <span>共 {{ total }} 条记录</span>
          <el-button
            @click="add"
            icon="el-icon-plus"
            size="small"
            type="primary"
            round
          >
            添加面经
          </el-button>
        </div>
      </template>
      <!-- table组件 -->
      <el-table :data="list" style="width: 100%">
        <el-table-column prop="stem" label="标题" width="400">
        </el-table-column>
        <el-table-column prop="creator" label="作者"> </el-table-column>
        <el-table-column prop="likeCount" label="点赞"> </el-table-column>
        <el-table-column prop="views" label="浏览数"> </el-table-column>
        <el-table-column prop="createdAt" label="更新时间" width="200">
        </el-table-column>

        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button
              type="text"
              icon="el-icon-view"
              @click="view(row.id)"
            ></el-button>
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="edit(row.id)"
            ></el-button>
            <el-button
              type="text"
              icon="el-icon-delete"
              @click="del(row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 底部分页组件 -->
      <el-pagination
        :page-size="pageSize"
        :current-page="current"
        background
        layout="prev, pager, next"
        :total="total"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </el-card>
    <!-- 抽屉组件 -->
    <el-drawer
      size="60%"
      :title="title"
      :visible.sync="drawer"
      :before-close="handleClose"
      @close="handleclose"
    >
      <!-- 抽屉内容区域 -->
      <!-- el-form表单 -->
      <el-form
        v-if="title !== '预览面经'"
        label-width="70px"
        :model="form"
        :rules="rules"
        ref="form"
      >
        <el-form-item label="标题" prop="stem">
          <el-input v-model="form.stem"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <!-- <el-input type="textarea"></el-input> -->
          <!-- 需要下载富文本插件 -->
          <!-- 富文本编辑器，校验单独处理 -->
          <quillEditor
            v-model="form.content"
            @blur="$refs.form.validateField('content')"
          ></quillEditor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleConfirm">确认</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
      <!-- 预览区域 -->
      <div v-else>
        <h3>{{ form.stem }}</h3>
        <el-divider></el-divider>
        <div v-html="form.content"></div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'
import {
  getArticleList,
  delArticle,
  addArticle,
  viewArticle,
  changeArticle
} from '@/api/article'
export default {
  components: {
    quillEditor
  },
  name: 'article-page',
  data() {
    return {
      // 列表数据相关
      current: 1,
      pageSize: 10,
      total: 0,
      list: [],
      // 抽屉相关
      drawer: false,
      title: '',
      form: {
        stem: '',
        content: ''
      },
      rules: {
        stem: [{ required: true, message: '标题不能为空', trigger: 'blur' }],
        content: [{ required: true, message: '内容不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    // 调用数据请求方法
    this.initData()
  },
  methods: {
    // 把获取列表数据请求写在方法内
    async initData() {
      const { data } = await getArticleList({
        current: this.current,
        pageSize: this.pageSize
      })
      console.log(data)
      this.list = data.rows
      this.total = data.total
    },
    // 改变页数 重新渲染
    handleCurrentChange(value) {
      console.log(value)
      this.current = value
      this.initData()
    },
    // 点击删除
    async del(id) {
      // 弹窗组件
      await this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      try {
        await delArticle(id)
        this.initData()
        this.$message.success('删除成功')
      } catch (error) {
        console.log(error)
      }
    },
    // 关闭抽屉？  async awite 怎么写
    async handleClose(done) {
      try {
        await this.$confirm('确认关闭？')
        // .then((_) => {
        done()
      } catch (error) {
        console.log(error)
      }
      // })
      // .catch((_) => {})
    },
    // 点击添加面经 弹出抽屉
    add() {
      this.drawer = true
      this.title = '添加面经'
    },
    // 点击确认按钮添加的
    async handleConfirm() {
      // 数据校验
      try {
        await this.$refs.form.validate()
        if (this.form.id) {
          // 编辑
          console.log(this.form)
          const newForm = {}
          newForm.id = this.form.id
          newForm.stem = this.form.stem
          newForm.content = this.form.content
          await changeArticle(newForm)
          this.$message.success('修改成功')
        } else {
          // 添加
          console.log(this.form)

          await addArticle(this.form)
          this.$message.success('添加成功')
        }
        this.initData()
        this.drawer = false
      } catch (err) {
        console.log(err)
      }
    },
    // 关闭抽屉 重置内容
    handleclose() {
      if (this.title !== '预览面经') {
        this.$refs.form.resetFields()
      }
    },
    // 点击编辑
    async edit(id) {
      this.title = '修改面经'
      const res = await viewArticle(id)
      // 这里把data给了form 所以上面可以用form.id
      this.form = res.data
      this.drawer = true // 打开抽屉
    },
    // 点击预览
    async view(id) {
      this.title = '预览面经'
      const res = await viewArticle(id)
      console.log(res)
      this.form.stem = res.data.stem
      this.form.content = res.data.content
      this.drawer = true
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
// 富文本编辑器最小高度
::v-deep .ql-editor {
  min-height: 200px;
}
::v-deep .el-drawer__body {
  padding: 20px;
}
</style>
